package com.dao.impl;

import com.commons.PageModel;
import com.dao.JsyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Tonight on 2017/3/15.
 */
@Repository
public class JsyDaoImpl implements JsyDao{

    @Autowired
    private DataSource dataSource;

    private Connection connection;

    private PreparedStatement preparedStatement;

    private ResultSet rs;
    @Override
    public PageModel<Map<String, Object>> implementSqlScript(PageModel<Map<String, Object>> pageModel) {

        Map<String,Object> resultMap = new HashMap();
        resultMap.put("result","success");
        try {
            connection =  dataSource.getConnection();
            preparedStatement = connection.prepareStatement(pageModel.getSql() + "  limit ?,? ");
            preparedStatement.setInt(1, (pageModel.getPageNo()-1) * pageModel.getPageSize());
            preparedStatement.setInt(2, pageModel.getPageNo() * pageModel.getPageSize());
            rs = preparedStatement.executeQuery();
            ResultSetMetaData rsm =rs.getMetaData();
            //获取每行数据的字段个数
            int columnCount = rsm.getColumnCount();
            //存放字段名
            List<String> listColumn = new ArrayList(columnCount);
            //存放数据
            List<List<Object>> dataList = new ArrayList<>();
            for (int index = 0;index < columnCount; index ++)
            {
                listColumn.add(rsm.getColumnName(index + 1));
            }
            while (rs.next()) {
                List<Object> itemList = new ArrayList<>();
                for (int i = 0; i < columnCount; i++)
                {
                    itemList.add(rs.getString(i + 1));
                }
                dataList.add(itemList);
            }
            resultMap.put("listColumn",listColumn);
            resultMap.put("dataList",dataList);

            ResultSet countRs = preparedStatement.executeQuery("select count(0) from (" + pageModel.getSql() + ") as item;");
            if(countRs.next())
            {
                pageModel.setTotalRecords(countRs.getInt(1));
            }

        }catch (Exception e)
        {
            resultMap.put("result","fail");
            resultMap.put("message",e.getMessage());
        }finally {
            pageModel.setData(resultMap);
            closeJdbc();
        }
        return pageModel;
    }

    void closeJdbc()
    {
        try {
            if(rs != null)
            {
                rs.close();
            }
            if(preparedStatement != null)
            {
                preparedStatement.close();
            }
            if(connection != null)
            {
                connection.close();
            }
        }catch (Exception e)
        {
           System.out.print("链接关闭失败！");
        }
    }
}
